#include "include.h"
using namespace irr;
using namespace std;

bool Game::runGame()
{
    //random number generator seeded with time
    srand((unsigned)time(0)); 
     
    // array for input buffer
    bool keys[irr::KEY_KEY_CODES_COUNT];
    // input class, listens for key presses
    InputReceiver listener(keys);

    // set the video device
    IrrlichtDevice* device = irr::createDevice(irr::video::EDT_SOFTWARE,
       irr::core::dimension2d<s32>(SCREEN_W,SCREEN_H), 16, false, false, false, &listener);

    // couldn't load driver, so bail out
    if (device == 0) return 1;

    // window titlebar text
    device->setWindowCaption(L"Spike vs. Mothma BETA");
    // set video driver
    video::IVideoDriver* driver = device->getVideoDriver();

    // create graphics objects
    Graphics display(device, driver);
    Graphics display2(device, driver);

    // create objects to be displayed
    DrawableObj back;
    Mothma mothma(P1_X_START,P1_Y_START, P1_W, P1_H);
    Spike spike(P2_X_START,P2_Y_START, P2_W, P2_H);
    Projectile note(mothma.getXPosition()+mothma.getXResolution()/2,mothma.getYPosition()+mothma.getYResolution()/2, P1_PROJ_W, P1_PROJ_H);
    Projectile spikeProj(spike.getXPosition()+spike.getXResolution()/2,spike.getYPosition()+spike.getYResolution()/2, P2_PROJ_W, P2_PROJ_H);
    DrawableObj winMothma;
    DrawableObj winSpike;
    HealthBar mothmaBar(50, 25, 220, 60);
    HealthBar spikeBar(710, 25, 220, 60);
    // add frames to each object.
    back.addFrames(1,"img/background.png", 0, SCREEN_W, SCREEN_H);
    note.addFrames(1,"img/note.png", 0, 32, 32);
    spikeProj.addFrames(1,"img/spikeproj.png", 0, 64, 32);
    winMothma.addFrames(1,"img/win-mothma.png", 0, 800, 600);
    winSpike.addFrames(1,"img/win-spike.png", 0, 800, 600);
    // HealthBar frames
    mothmaBar.addFrames(1,"img/bar/MothmaBar_100.png", 0, 220, 60);
    mothmaBar.addFrames(1,"img/bar/MothmaBar_90.png", 0, 220, 60);
    mothmaBar.addFrames(1,"img/bar/MothmaBar_80.png", 0, 220, 60);
    mothmaBar.addFrames(1,"img/bar/MothmaBar_70.png", 0, 220, 60);
    mothmaBar.addFrames(1,"img/bar/MothmaBar_60.png", 0, 220, 60);
    mothmaBar.addFrames(1,"img/bar/MothmaBar_50.png", 0, 220, 60);
    mothmaBar.addFrames(1,"img/bar/MothmaBar_40.png", 0, 220, 60);
    mothmaBar.addFrames(1,"img/bar/MothmaBar_30.png", 0, 220, 60);
    mothmaBar.addFrames(1,"img/bar/MothmaBar_20.png", 0, 220, 60);
    mothmaBar.addFrames(1,"img/bar/MothmaBar_10.png", 0, 220, 60);
    mothmaBar.addFrames(1,"img/bar/MothmaBar_0.png", 0, 220, 60);
    spikeBar.addFrames(1,"img/bar/SpikeBar_100.png", 0, 220, 60);
    spikeBar.addFrames(1,"img/bar/SpikeBar_90.png", 0, 220, 60);
    spikeBar.addFrames(1,"img/bar/SpikeBar_80.png", 0, 220, 60);
    spikeBar.addFrames(1,"img/bar/SpikeBar_70.png", 0, 220, 60);
    spikeBar.addFrames(1,"img/bar/SpikeBar_60.png", 0, 220, 60);
    spikeBar.addFrames(1,"img/bar/SpikeBar_50.png", 0, 220, 60);
    spikeBar.addFrames(1,"img/bar/SpikeBar_40.png", 0, 220, 60);
    spikeBar.addFrames(1,"img/bar/SpikeBar_30.png", 0, 220, 60);
    spikeBar.addFrames(1,"img/bar/SpikeBar_20.png", 0, 220, 60);
    spikeBar.addFrames(1,"img/bar/SpikeBar_10.png", 0, 220, 60);
    spikeBar.addFrames(1,"img/bar/SpikeBar_0.png", 0, 220, 60);

    bool gameOver = false;
    // main game loop
    while (device->run() && driver)
    {
        driver->beginScene(true, false, video::SColor(255, 0, 0, 0));  
        mothma.animate();
        spike.animate();
        // if the game isn't over
        if(gameOver == false)
        {
            
            // Spike input
            if(keys[irr::KEY_NUMPAD0])
            { 
              if(keys[irr::KEY_NUMPAD4] || keys[irr::KEY_NUMPAD6] || Collision::checkCollision(mothma,spike,10) )
              {
                spike.setState("biting"); 
                if(Collision::checkCollision(mothma,spike,10))
                {
                  mothma.decrementHitPoints();
                  mothmaBar.incrementFrame();
                }
              }
              else
              {  
                if (spike.spikes.size() < 2)                      
                spike.spikes.push_back(new SpikeNeedle(spike.getXPosition()+spike.getXResolution()/2,spike.getYPosition()+spike.getYResolution()/2)); 
              }
            }
            else if(!keys[irr::KEY_NUMPAD0] && spike.getHitPoints())
              spike.setState("float");   
                     
            if(keys[irr::KEY_NUMPAD5] && !Collision::checkBoundryCollision(spike, back,10))
                spike.moveObject(0,+Y_MOV*3 );
            if(keys[irr::KEY_NUMPAD8] && !Collision::checkBoundryCollision(spike, back,10))
                spike.moveObject(0,-Y_MOV*3);
                
            if(keys[irr::KEY_NUMPAD6] && !Collision::checkBoundryCollision(spike, back,10))
            {
                spike.moveObject(X_MOV*3,0);
                if(!spikeProj.getIsVisible())
                spikeProj.setCoords(spike.getXPosition()+spike.getXResolution()/2,(spike.getYPosition()+spike.getYResolution()/2));
            }
            //if(keys[irr::KEY_NUMPAD4] && !Collision::checkCollision(mothma,spike,10))
            if(keys[irr::KEY_NUMPAD4])
            {
                spike.moveObject(-X_MOV*3,0);
                if(!spikeProj.getIsVisible())
                spikeProj.setCoords(spike.getXPosition()+spike.getXResolution()/2,(spike.getYPosition()+spike.getYResolution()/2));
            }
            // Mothma input
            if(keys[irr::KEY_KEY_S]){
                mothma.setState("attacking");
                if (mothma.notes.size() < 10)
                  mothma.notes.push_back(new MusicNote(mothma.getXPosition()+mothma.getXResolution()/2,mothma.getYPosition()+mothma.getYResolution()/2));
                //note.setIsVisible(true);
            }
            if(keys[irr::KEY_KEY_W])
                mothma.setJumping(true);
                
            //if(keys[irr::KEY_KEY_D] && !Collision::checkCollision(mothma,spike,10))
            if(keys[irr::KEY_KEY_D])
            {
                //mothma.moveObject(X_MOV,0);
                if(!note.getIsVisible())
                note.setCoords(mothma.getXPosition()+mothma.getXResolution()/2,(mothma.getYPosition()+mothma.getYResolution()/2));
                mothma.setState("mov_right");
            }
            if(keys[irr::KEY_KEY_A] && !Collision::checkBoundryCollision(mothma, back,10))
            {
                //mothma.moveObject(-X_MOV,0);
                if(!note.getIsVisible())
                note.setCoords(mothma.getXPosition()+mothma.getXResolution()/2,(mothma.getYPosition()+mothma.getYResolution()/2));
                mothma.setState("mov_left");
            }
            if(keys[irr::KEY_KEY_X] && !keys[irr::KEY_KEY_S])
              mothma.setState("block"); 
            else if(!keys[irr::KEY_KEY_A] && !keys[irr::KEY_KEY_D] && (!keys[irr::KEY_KEY_X]) && mothma.getHitPoints())
            {  
              if (!keys[irr::KEY_KEY_S]) 
                mothma.setState("stand");
            }
              
        }
            //driver->beginScene(true, false, video::SColor(255, 0, 0, 0));
            
            // draw a static background
            display.draw(back.getFrameVector(),0,0,0);
            // draw spikes projectile
            // if it hits a window boundry, make projectile go away and reset the position
            // if it hits a player, make projectile go away, reset the position and decrement
            // opponents hit points
            
            for (int i = 0;i < mothma.notes.size(); i++)
            {
                for (int j = 0; j < spike.spikes.size(); j++)
                {
                  if(Collision::checkCollision(*spike.spikes[j],*mothma.notes[i]) == true)
                  {
                    delete mothma.notes[i];  
                    mothma.notes.erase(mothma.notes.begin()+i);
                    delete spike.spikes[j];  
                    spike.spikes.erase(spike.spikes.begin()+j);
                  }  
                } 
            }
            // collision check for mothma notes via for loop
            for (int i = 0; i < mothma.notes.size(); i++)
            {
              mothma.notes[i]->animate((rand()%3)+1);
              display.draw(mothma.notes[i]->getFrameVector(),mothma.notes[i]->getXPosition(),mothma.notes[i]->getYPosition(),0);
              if(Collision::checkCollision(spike,*mothma.notes[i]) == true)
              {
                spike.decrementHitPoints();
                delete mothma.notes[i];  
                mothma.notes.erase(mothma.notes.begin()+i);
                if(spikeBar.getCurrentFrame() < 10)
                  spikeBar.incrementFrame();
              }
              else if (mothma.notes[i]->getXPosition() > 1000 || mothma.notes[i]->getYPosition() > 600){
                delete mothma.notes[i];  
                mothma.notes.erase(mothma.notes.begin()+i);
              }
            }
            // collision check for spike needles via for loop
            for (int i = 0; i < spike.spikes.size(); i++){
              spike.spikes[i]->animate((rand()%3)+1);
              display.draw(spike.spikes[i]->getFrameVector(),spike.spikes[i]->getXPosition(),spike.spikes[i]->getYPosition(),0);
              if(Collision::checkCollision(mothma,*spike.spikes[i]) == true)
              {
                if (mothma.getState() != "block")                                                    
                  mothma.decrementHitPoints();
                delete spike.spikes[i];  
                spike.spikes.erase(spike.spikes.begin()+i);
                if(mothmaBar.getCurrentFrame() < 10 && mothma.getState()!= "block")
                  mothmaBar.incrementFrame();
              }
              else if (spike.spikes[i]->getXPosition() < 0 || spike.spikes[i]->getYPosition() < 0){
                delete spike.spikes[i];  
                spike.spikes.erase(spike.spikes.begin()+i);
              }
            }
            
            // draw two player sprites
            display.draw(mothma.getFrameVector(),mothma.getXPosition(),mothma.getYPosition(),&mothma,device->getTimer()->getTime());
            display2.draw(spike.getFrameVector(),spike.getXPosition(),spike.getYPosition(),&spike,device->getTimer()->getTime());
            
            // Draw mothma's healthbar
            display.draw(mothmaBar.getFrameVector(),mothmaBar.getXPosition(),mothmaBar.getYPosition(),mothmaBar.getCurrentFrame());
            // Draw spike's healthbar
            display.draw(spikeBar.getFrameVector(),spikeBar.getXPosition(),spikeBar.getYPosition(),spikeBar.getCurrentFrame());
            
            // check if either player is dead, if either is, display which player wins
            // and set gameOver so the game stops.
            if(mothma.getHitPoints() <= 0)
            {
                mothma.setState("dying");
                spike.setState("float");
                display.draw(winSpike.getFrameVector(),100,0,0);
                gameOver = true;
            }
            if(spike.getHitPoints() <= 0)
            { 
                spike.setState("dying");                    
                mothma.setState("stand");
                display.draw(winMothma.getFrameVector(),100,0,0);
                gameOver = true;
            }
            

        driver->endScene();
    }

    device->drop();
    return true;
}


